package com.example.a10928.weifriend.ui.login

import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.text.Editable
import android.text.TextUtils
import android.text.TextWatcher
import com.example.a10928.weifriend.App
import com.example.a10928.weifriend.R
import com.example.a10928.weifriend.http.api.RowsWrapper
import com.example.a10928.weifriend.http.api.VariableName
import com.example.a10928.weifriend.ui.BaseActivity
import com.example.a10928.weifriend.util.RxUtil
import kotlinx.android.synthetic.main.activity_phone_register.*
import java.util.function.Consumer

class PhoneRegisterActivity : BaseActivity() {

    var clickable:Boolean=false

    override fun getContentView(): Int {
        return R.layout.activity_phone_register
    }

    override fun initView() {

        supportActionBar?.hide()
        et_phone.addTextChangedListener(
                object : TextWatcher {
                    override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {}
                    override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {}
                    override fun afterTextChanged(s: Editable?) {
                        change()

                    }}
        )

        tv_next.setOnClickListener {
            if(clickable){
                if(!TextUtils.isEmpty(et_phone.text.toString())){

                    App.app().bpService.checkPhoneRegister(et_phone.text.toString())
                            .compose(RxUtil.io2mainLoad(this))
                            .subscribe(
                                    io.reactivex.functions.Consumer {
                                        var intent= Intent(this,CodeActivity::class.java)

                                        intent.putExtra(VariableName.PHONE,et_phone.text.toString())
                                        startActivity(intent)
                                        finish()
                                    },
                                    io.reactivex.functions.Consumer {
                                        RxUtil.errorException(it)
                                    }
                            )



                }else{
                }
            }
        }
        tv_login.setOnClickListener {
            var intent=Intent(this,LoginActivity::class.java)
            startActivity(intent)
            finish()
        }

    }

    override fun initData() {

    }

    fun change(){
        if(!TextUtils.isEmpty(et_phone.text.toString())&&et_phone.text.toString().length==11){

            clickable=true
            tv_next.setBackgroundResource(R.drawable.input)
        }else{
            clickable=false
            tv_next.setBackgroundResource(R.drawable.uninput)
        }
    }

}
